package com.shenmazong.grade.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.grade.entity.TbTaskGrade;
import com.shenmazong.grade.entity.TbTaskSkillGradeDetail;
import com.shenmazong.grade.entity.TbTaskTheoryGradeDetail;
import com.shenmazong.grade.service.TaskGradeService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.*;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.shenmazong.config.MqConfig.TASK_START_TEST_QUEUE;
import static com.shenmazong.config.MqConfig.TASK_SUBMIT_QUEUE;


/**
 * @description 作业成绩管理控制层
 * @author Qi
 * @date 2021/9/18 16:40
 * @version 1.0
 */

@Api(tags = "作业成绩管理")
@RestController
@RequestMapping("grade")
public class TaskGradeController {

    @Autowired
    private TaskGradeService gradeService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * @description 作业成绩新增
     * @params [tbTaskGrade]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:16
     */

    @PostMapping("saveTaskGrade")
    public ResultResponse saveTaskGrade(@RequestBody TbTaskGrade tbTaskGrade){
        return gradeService.saveTaskGrade(tbTaskGrade);
    }

    /**
     * @description 作业成绩批量删除
     * @params [chooseIdVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:16
     */
    @PostMapping("deleteTaskGrade")
    public ResultResponse deleteTaskGrade(@RequestBody BatchChooseIdVo chooseIdVo){
        return gradeService.deleteTaskGrade(chooseIdVo);
    }

    /**
     * @description 作业成绩修改
     * @params [tbTaskGrade]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("updateTaskGrade")
    public ResultResponse updateTaskGrade(@RequestBody TbTaskGrade tbTaskGrade){
        return gradeService.updateTaskGrade(tbTaskGrade);
    }

    /**
     * @description 作业成绩列表
     * @params [paramVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("listTaskGrade")
    public ResultResponse listTaskGrade(@RequestBody ListParamVo paramVo){
        return gradeService.listTaskGrade(paramVo);
    }

    /**
     * @description 作业成绩根据ID查询
     * @params [tbTaskGrade]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("getTaskGrade")
    public ResultResponse getTaskGrade(@RequestBody TbTaskGrade tbTaskGrade){
        return gradeService.getTaskGrade(tbTaskGrade);
    }

    /**
     * @description 作业理论成绩明细新增
     * @params [tbTaskTheoryGradeDetail]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("saveTaskTheoryGradeDetail")
    public ResultResponse saveTaskTheoryGradeDetail(@RequestBody TbTaskTheoryGradeDetail tbTaskTheoryGradeDetail){
        return gradeService.saveTaskTheoryGradeDetail(tbTaskTheoryGradeDetail);
    }

    /**
     * @description 作业理论成绩明细批量删除
     * @params [chooseIdVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("deleteTaskTheoryGradeDetail")
    public ResultResponse deleteTaskTheoryGradeDetail(@RequestBody BatchChooseIdVo chooseIdVo){
        return gradeService.deleteTaskTheoryGradeDetail(chooseIdVo);
    }

    /**
     * @description 作业理论成绩明细修改
     * @params [tbTaskTheoryGradeDetail]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("updateTaskTheoryGradeDetail")
    public ResultResponse updateTaskTheoryGradeDetail(@RequestBody TbTaskTheoryGradeDetail tbTaskTheoryGradeDetail){
        return gradeService.updateTaskTheoryGradeDetail(tbTaskTheoryGradeDetail);
    }

    /**
     * @description 作业理论成绩明细列表
     * @params [paramVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("listTaskTheoryGradeDetail")
    public ResultResponse listTaskTheoryGradeDetail(@RequestBody ListParamVo paramVo){
        return gradeService.listTaskTheoryGradeDetail(paramVo);
    }

    /**
     * @description 作业技能成绩明细新增
     * @params [tbTaskSkillGradeDetail]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("saveTaskSkillGradeDetail")
    public ResultResponse saveTaskSkillGradeDetail(@RequestBody TbTaskSkillGradeDetail tbTaskSkillGradeDetail){
        return gradeService.saveTaskSkillGradeDetail(tbTaskSkillGradeDetail);
    }

    /**
     * @description 作业技能成绩明细批量删除
     * @params [chooseIdVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("deleteTaskSkillGradeDetail")
    public ResultResponse deleteTaskSkillGradeDetail(@RequestBody BatchChooseIdVo chooseIdVo){
        return gradeService.deleteTaskSkillGradeDetail(chooseIdVo);
    }

    /**
     * @description 作业技能成绩明细修改
     * @params [tbTaskSkillGradeDetail]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:17
     */
    @PostMapping("updateTaskSkillGradeDetail")
    public ResultResponse updateTaskSkillGradeDetail(@RequestBody TbTaskSkillGradeDetail tbTaskSkillGradeDetail){
        return gradeService.updateTaskSkillGradeDetail(tbTaskSkillGradeDetail);
    }

    /**
     * @description 作业技能成绩明细列表
     * @params [paramVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author Qi
     * @date 2021/9/21 17:18
     */
    @PostMapping("listTaskSkillGradeDetail")
    public ResultResponse listTaskSkillGradeDetail(@RequestBody ListParamVo paramVo){
        return gradeService.listTaskSkillGradeDetail(paramVo);
    }

    //理论自动判卷
    @PostMapping("taskTheoryAutoMake")
    public ResultResponse taskTheoryAutoMake(@RequestBody TaskStudentSubmitVo submitVo){

        return gradeService.taskTheoryAutoMake(submitVo);
    }

    //作业内容到时间自动提交
    @PostMapping("autoTaskSubmit")
    public ResultResponse autoTaskSubmit(@RequestBody TaskStudentSubmitVo submitVo){
        return gradeService.taskTheoryAutoMake(submitVo);
    }

    //作业内容手动提交
    @PostMapping("manualTaskSubmit")
    public ResultResponse manualTaskSubmit(@RequestBody TaskStudentSubmitVo submitVo){
        //判断考试状态
        String statusKey = "task_status_"+submitVo.getLayoutId();
        if(redisTemplate.hasKey(statusKey) && "1".equals(redisTemplate.opsForValue().get(statusKey))){
            return gradeService.taskTheoryAutoMake(submitVo);
        }else {
            return ResultResponse.FAILED("考试已结束,交卷失败!");
        }
    }


    //获取试题数据的测试方法
    @PostMapping("aaaa")
    public ResultResponse aaaa(){

        List<TheoryTestVo> list = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            //获取考试试题
            String contentKey = "task_content_1443091077925031937";
            //解析JSON字符串
            String content = redisTemplate.opsForValue().get(contentKey);
            list = mapper.readValue(content, new TypeReference<List<TheoryTestVo>>() {
            });

        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return ResultResponse.FAILED("试题解析失败,请联系管理员");
        }

        List<TheoryAnswerVo> arrayList = new ArrayList<>();
        for (TheoryTestVo testVo : list) {
            TheoryAnswerVo answerVo = new TheoryAnswerVo();
            answerVo.setTheoryId(testVo.getTheoryId());
            answerVo.setStudentChoose(testVo.getAnswers());
            arrayList.add(answerVo);
        }
        return ResultResponse.SUCCESS(arrayList);
    }
}




